﻿using System;
using System.Collections;
using System.Drawing;
using System.Collections.Generic;
using System.Text;

namespace LowLevelGraphics
{
    /// <summary>
    /// Class to provide functionality for sorting points
    /// </summary>
    public class PointSort : IComparer<Point>
    {
        public enum Mode
        {
            X,
            Y,
            BothUp, BothDown
        }

        Mode currentMode = Mode.X;

        /// <summary>
        /// Initializes a new instance of the <see cref="PointSort"/> class.
        /// </summary>
        /// <param name="mode">The mode.</param>
        public PointSort(Mode mode)
        {
            currentMode = mode;
        }

        #region IComparer<Point> Member

        /// <summary>
        /// Compares two objects and returns a value indicating whether one is less than, equal to, or greater than the other.
        /// </summary>
        /// <param name="x">The first object to compare.</param>
        /// <param name="y">The second object to compare.</param>
        /// <returns>
        /// Value Condition Less than zero<paramref name="x"/> is less than <paramref name="y"/>.Zero<paramref name="x"/> equals <paramref name="y"/>.Greater than zero<paramref name="x"/> is greater than <paramref name="y"/>.
        /// </returns>
        public int Compare(Point x, Point y)
        {
            Point point1 = x;
            Point point2 = y;

            if (currentMode == Mode.X) //Compare X values
            {
                if (point1.X > point2.X)
                    return 1;
                else if (point1.X < point2.X)
                    return -1;
                else
                    return 0;
            }
            else if (currentMode == Mode.Y)
            {
                if (point1.Y > point2.Y) //Compare Y Values
                    return 1;
                else if (point1.Y < point2.Y)
                    return -1;
                else
                    return 0;
            }
            else
            {
                int n1P = point1.X * 100 + point1.Y;
                int n2P = point2.X * 100 + point2.Y;

                if (currentMode == Mode.BothDown)
                {
                    return n1P.CompareTo(n2P);
                }
                else if (currentMode == Mode.BothUp)
                {
                    return n2P.CompareTo(n1P);
                }
            }
            return 0;
        }

        #endregion
    }
}
